﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace CSharpHomework7_1
{
    class Program
    {
        static void TestId(string id)
        {
            string pattern = @"^\d{17}(?:\d|x)$";
            string birth = id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();

            int[] arrWeight = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            string[] lastId = { "1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2" };

            int sum = 0;
            for(int i = 0; i < 17; i++)
            {
                sum += arrWeight[i] * int.Parse(id[i].ToString());
            }

            int result = sum % 11;

            if (Regex.IsMatch(id, pattern))
            {
                if(DateTime.TryParse(birth,out time))
                {
                    if (lastId[result] == id[17].ToString()) { Console.WriteLine("身份证号格式正确！"); }
                    else { Console.WriteLine("最后一位校验错误！"); }
                }
                else
                {
                    Console.WriteLine("出生日期验证失败！");
                }
            }
            else { Console.WriteLine("身份证号格式失误！"); }
        }

        static void Main(string[] args)
        {
            string id;
            id = Console.ReadLine();
            TestId(id);
            Console.WriteLine("");
        }
    }
}
